//
//  Root_AppleTVViewController.m
//  classicFilms
//
//  Created by 刘继丹 on 17/3/14.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "Root_AppleTVViewController.h"
#import "AppTVTopLoopView.h"
#import "AppTVDetailController.h"
#import "ServiceListApi.h"
#import "AppTvPlhoderView.h"
#import "YTKBaseRequest+AnimatingAccessory.h"

@interface Root_AppleTVViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger _section_count;
}
@property(nonatomic, strong) AppTvPlhoderView *mAppTvPlhoderView;//占位

@property(nonatomic, strong) AppTVTopLoopView *mAppTVTopLoopView;//顶部轮播图

@property(nonatomic, strong) UITableView *mTableView;

@property(nonatomic, strong) NSArray *mImageArr;

@property(nonatomic, strong) NSArray *mWorkCateArr;

@property(nonatomic, strong) ServiceListResponse *mRecommendList, *mSeniorList, *mGoldMedalList, *mChiefList;

@end

@implementation Root_AppleTVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = kBackGroundColor_blue;
    
    [self.view addSubview:self.mTableView];
    
    if (_appleTVType == AppleTVType_silentfilms) {
        
        _section_count = 4;

        self.mWorkCateArr = @[@"礼成推荐",  @"首席主持人", @"金牌主持人", @"资深主持人"];

        self.mImageArr = @[@"compere"];

        //主持人
        
        //推荐
        [self loadNewData:@"1" type:@"recommend" type_id:@"1"];
        
        //首席
        [self loadNewData:@"1" type:@"level" type_id:@"1"];
        
        //资深
        [self loadNewData:@"1" type:@"level" type_id:@"2"];
        
        //金牌
        [self loadNewData:@"1" type:@"level" type_id:@"3"];
        
    }else if (_appleTVType == AppleTVType_comedyFilms){
        
        _section_count = 4;
        
        self.mImageArr = @[ @"dresser"];

        self.mWorkCateArr = @[@"礼成推荐",  @"首席化妆师", @"金牌化妆师", @"资深化妆师"];
        
        //化妆师
        
        //推荐
        [self loadNewData:@"4" type:@"recommend" type_id:@"1"];
        
        //首席
        [self loadNewData:@"4" type:@"level" type_id:@"1"];
        
        //资深
        [self loadNewData:@"4" type:@"level" type_id:@"2"];
        
        //金牌
        [self loadNewData:@"4" type:@"level" type_id:@"3"];
 
        
    }else if (_appleTVType == AppleTVType_filmNoir){
        
        _section_count = 1;
        
        self.mImageArr = @[ @"shoot"];

        
        self.mWorkCateArr = @[@"礼成推荐", @"首席摄影师", @"金牌摄影师", @"资深摄影师"];
        
        //摄影师
        
        //推荐
        [self loadNewData:@"3" type:@"recommend" type_id:@"1"];
        
        //首席
        [self loadNewData:@"3" type:@"level" type_id:@"1"];
        
        //资深
        [self loadNewData:@"3" type:@"level" type_id:@"2"];
        
        //金牌
        [self loadNewData:@"3" type:@"level" type_id:@"3"];
     
    }else if (_appleTVType == AppleTVType_sciFiHorror){
        
        _section_count = 1;
        
        self.mImageArr = @[@"camera"];

        self.mWorkCateArr = @[@"礼成推荐",  @"首席摄像师",  @"金牌摄像师", @"资深摄像师"];

        //摄像师
        
        //推荐
        [self loadNewData:@"2" type:@"recommend" type_id:@"1"];
        
        //首席
        [self loadNewData:@"2" type:@"level" type_id:@"1"];
        
        //资深
        [self loadNewData:@"2" type:@"level" type_id:@"2"];
        
        //金牌
        [self loadNewData:@"2" type:@"level" type_id:@"3"];
        
    }
    
//    else{
//    
//        [self.view addSubview:self.mAppTvPlhoderView];
//        
//    }
    
}

#pragma mark- request
-(void)loadNewData:(NSString *)cate type:(NSString *)type type_id:(NSString *)type_id{
   
    ServiceListApi *serviceList = [[ServiceListApi alloc]initWithCate:cate type:type type:type_id];
    serviceList.animatingText = nil;
    serviceList.animatingView = self.view;
    
    [serviceList startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
//        NSLog(@"🍏=====%@ === %@", cate, request.responseJSONObject);
        
        if ([type isEqualToString:@"recommend"]) {
            
            _mRecommendList = request.responseModelObject;
            
        }else{
        
            if ([type_id integerValue] == 1) {
                
                _mChiefList = request.responseModelObject;
                
            }else if ([type_id integerValue] == 2){
                
                _mSeniorList = request.responseModelObject;

            }else if ([type_id integerValue] == 3){
               
                _mGoldMedalList = request.responseModelObject;
                
            }
            
        }
        
        [_mTableView reloadData];
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
    
}

#pragma mark- tableView delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _section_count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 416;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kLoopViewHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.mAppTVTopLoopView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger indexRow = indexPath.row;
    
    AppTVListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AppTVListTableCell" forIndexPath:indexPath];
    
    cell.appleTVType = _appleTVType;
    
    cell.titleStr = _mWorkCateArr[indexPath.row];
    
    if (indexRow == 0) {
        cell.itemArr = _mRecommendList.data.lists;
    }else if (indexRow == 1){
        cell.itemArr = _mChiefList.data.lists;
    }else if (indexRow == 2){
        cell.itemArr = _mGoldMedalList.data.lists;
    }else if (indexRow == 3){
        cell.itemArr = _mSeniorList.data.lists;
    }
    
    //图片选择
    __weak typeof(self) weakSelf = self;
    cell.imageSelectBlock = ^(NSInteger selectIndex){
        
        ListModel *listModel;
        
        if (indexRow == 0) {
            listModel = weakSelf.mRecommendList.data.lists[selectIndex];
        }else if (indexRow == 1){
            listModel = weakSelf.mChiefList.data.lists[selectIndex];
        }else if (indexRow == 2){
            listModel = weakSelf.mGoldMedalList.data.lists[selectIndex];
        }else if (indexRow == 3){
            listModel = weakSelf.mSeniorList.data.lists[selectIndex];
        }
        
        AppTVDetailController *detail = [[AppTVDetailController alloc]init];
        detail.list_id = listModel.list_id;
         if (weakSelf.appleTVType == AppleTVType_silentfilms || weakSelf.appleTVType == AppleTVType_sciFiHorror )
         {
             detail.type = 0;
         }else
         {
             detail.type = 1;
         }
         
        [weakSelf presentViewController:detail animated:YES completion:nil];
        
    };
    
    return cell;
}

#pragma mark- configView
-(AppTVTopLoopView *)mAppTVTopLoopView{
    if (!_mAppTVTopLoopView) {
        _mAppTVTopLoopView = [[AppTVTopLoopView alloc]initWithFrame:CGRectMake(0, 0, KVIEW_W, kLoopViewHeight) type:AppTVLoopViewType_top];
        _mAppTVTopLoopView.imageArr = _mImageArr;
        
        _mAppTVTopLoopView.scrollIndex = _appleTVType;
        
        __weak typeof(self) weakSelf = self;

        _mAppTVTopLoopView.imageSelectBlock = ^(NSInteger selectIndex){
        
            AppTVDetailController *detail = [[AppTVDetailController alloc]init];
            
            if (_appleTVType==AppleTVType_silentfilms) {
                detail.list_id = @"12";
            }else if (_appleTVType==AppleTVType_comedyFilms){
                detail.list_id = @"21";
            }else if (selectIndex==2){
              
            }else if (selectIndex==3){
               
            }
            if (weakSelf.appleTVType == AppleTVType_silentfilms || weakSelf.appleTVType == AppleTVType_sciFiHorror )
            {
                detail.type = 0;
                
            }else
            {
                detail.type = 1;
            }
            
            [weakSelf presentViewController:detail animated:YES completion:nil];
            
        };
        
        
    }
    return _mAppTVTopLoopView;
}

-(UITableView *)mTableView{
    if (!_mTableView) {
        _mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KVIEW_W, KVIEW_H) style:UITableViewStylePlain];
        _mTableView.delegate = self;
        _mTableView.dataSource = self;
        _mTableView.showsVerticalScrollIndicator = NO;
        _mTableView.allowsSelection = NO;
        [_mTableView setContentInset:UIEdgeInsetsMake(KTABBAR_H, 0, 50, 0)];
        _mTableView.backgroundColor = [UIColor clearColor];
        [_mTableView registerClass:[AppTVListTableCell class] forCellReuseIdentifier:@"AppTVListTableCell"];
    }
    return _mTableView;
}

-(AppTvPlhoderView *)mAppTvPlhoderView{
    if (!_mAppTvPlhoderView) {
        _mAppTvPlhoderView = [[AppTvPlhoderView alloc]initWithFrame:self.view.frame];
    }
    return _mAppTvPlhoderView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
